package com.example.thread.d8CompletableFuture1.supplyAsync;

import com.example.thread.d8CompletableFuture1.SmallTool;

import java.util.concurrent.CompletableFuture;
// 多个线程同时进行
// 用来合并两个任务，结果由合并函数（BiFunction)返回
public class _03_thenCombine {
    public static void main(String[] args) {
        SmallTool.printTimeAndThread("小白进入餐厅");
        SmallTool.printTimeAndThread("小白点了 番茄炒鸡蛋 + 一碗米饭");

        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(()-> {
            SmallTool.printTimeAndThread("厨师炒菜");
            SmallTool.sleepMillis(200);
            return "番茄炒蛋";
        }).thenCombine(CompletableFuture.supplyAsync(()-> { // dish是上一个任务的返回值
            SmallTool.printTimeAndThread("服务员蒸饭");
            SmallTool.sleepMillis(100);
            return "米饭";
            // dish,rice为前面两个线程的返回值
        }),(dish,rice)-> {
            SmallTool.printTimeAndThread("服务员打饭");
            SmallTool.sleepMillis(100);
            return String.format("%s + %s 好了", dish,rice);
        });

        SmallTool.printTimeAndThread("小白在打王者");
        SmallTool.printTimeAndThread(String.format("%s,小白开吃",cf1.join()));  // join（）等待任务执行结束，返回任务结束的结果
    }
}
